package com.shardings.selfdefined;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingAlgorithm;
import org.apache.shardingsphere.sharding.api.sharding.complex.ComplexKeysShardingValue;

import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import java.util.Optional;

/**
 * @author: LCG
 * @date: 2022-11-12 18:52:43
 * @description: 自定义 多字段分片算法 按照数据库划分
 **/
@Slf4j
public class MyComplexShardingAlgorithmDB implements ComplexKeysShardingAlgorithm<String> {

    private static final String column1="buMen";

    private static final String column2="sex";

    /**
     * @author: LCG
     * @date: 2022-11-12 23:19:16
     * @description:  collection  数据库的逻辑名称集合
     *                complexKeysShardingValue  复合分片字段的字段名和字段值
     */
    @Override
    public Collection<String> doSharding(Collection<String> collection, ComplexKeysShardingValue<String> complexKeysShardingValue) {

        String logicTableName = complexKeysShardingValue.getLogicTableName();

        Map<String, Collection<String>> columnNameAndShardingValuesMap = complexKeysShardingValue.getColumnNameAndShardingValuesMap();

        Collection<String> c1 = columnNameAndShardingValuesMap.get(column1);

        Collection<String> c2 = columnNameAndShardingValuesMap.get(column2);

        if(Optional.ofNullable(c1).isPresent()&&Optional.ofNullable(c2).isPresent()){

            StringBuffer sb=new StringBuffer(c1.stream().findFirst().get());
            sb.append(c2.stream().findFirst().get());

            int i = sb.toString().hashCode() % collection.size();

            String s = i == 0 ? "d0" : "d1";

            return Arrays.asList(s);
        }

        return collection;
    }


    @Override
    public void init() {

    }

    @Override
    public String getType() {
        return "COMPLEX-DB";
    }
}
